<template>
  <div class="pre">
    <el-button type="primary" plain @click="addScript">添加前置脚本</el-button>
    <el-tree :data="treeData" :allow-drop="allowDrop" :allow-drag="allowDrag" draggable default-expand-all node-key="id"
      @node-drop="handleDrop" :expand-on-click-node="false" :props="defaultProps" :indent="22"
      @node-click="handleNodeClick" class="pre-tree" ref="treeRef" :check-on-click-node="true">
      <template #default="{ node, data }">
        <span class="pre-tree__node">
          <span class="label">
            <el-icon>
              <Operation />
            </el-icon>
            <span>{{ node.label }}</span>
          </span>
          <span class="operation">
            <el-popover class="operation-popover" v-if="popoverVisible" @before-leave="hidePopver">
              <template #reference>
                <el-icon>
                  <MoreFilled />
                </el-icon>
              </template>
              <div class="operation-list">
                <p @click.stop="edit(data)"> <el-icon>
                    <Edit />
                  </el-icon> 编辑 </p>
                <p @click.stop="remove(data)"><el-icon>
                    <Remove />
                  </el-icon> 删除 </p>
              </div>
            </el-popover>
          </span>
        </span>
      </template>
    </el-tree>
    <UpdateCustomScript ref="updateCustomScriptRef"/>
  </div>
</template>

<script setup lang="ts">
import * as DebugApi from '@/api/debug'
import { MoreFilled, Operation, Edit, Remove } from '@element-plus/icons-vue'
import { useAppStore } from '@/store/modules/app'
import UpdateCustomScript from '../debug/dialog/prepost/UpdateCustomScript.vue';
const appStore = useAppStore()
const projectId = computed(() => appStore.getSelectProject)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const treeData = ref([{
  name: 'aa'
},{
  name: 'bb'
}] as any)
const emit = defineEmits([])
const defaultProps = {
  children: 'children',
  label: 'name'
}
const treeRef = ref()
const updateCustomScriptRef = ref()
const loading = ref(false)
const popoverVisible = ref(true)

/** 获取脚本列表 */
const getScriptData = () => {

}
/** 点击脚本记录 */
const handleNodeClick = () => {

}
/** 添加脚本 */
const addScript = ()=>{
  updateCustomScriptRef.value.open('create')
} 
/**
 * 重命名
 * */
const edit = (data) => {
  updateCustomScriptRef.value.open('edit',data)
}
/**
 * 删除
 * */
const remove = async (data) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    loading.value = true
    // 发起删除
    await DebugApi.deleteApiInTree({ id: data.id, projectId: projectId.value })
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getScriptData()
  } catch (error) {
    console.log(error)
  } finally {
    loading.value = false
  }
}
/** 拖动树后保存 */
const handleDrop = async (draggingNode: Node, dropNode: Node, position) => {

}
/** 只有目录可以被拖入 */
const allowDrop = () => {
  return true
}
/** 只有接口、目录可以被拖动 */
const allowDrag = () => {
  return true
}
/**
 * 避免popover闪现bug
 * */
const hidePopver = () => {
  popoverVisible.value = false
  setTimeout(() => {
    popoverVisible.value = true
  }, 10)
}
</script>

<style scoped lang="scss">
.pre {
  padding-right: 10px;
}

.pre-tree {
  overflow: auto;
  height: calc(100% - 22px);
  margin-top: 10px;

  :deep(.el-tree-node__content) {
    border: 1px solid var(--el-border-color-light);
    border-radius: 5px;
    padding: 5px;
    margin-bottom: 5px;

    .pre-tree__node {
      display: flex;
      justify-content: space-between;
      width: calc(100% - 20px);
      padding: 0 10px;
    }

    .operation-list {
      p {
        display: flex;
        align-items: center;
        border-bottom: 1px solid #ccc;
        padding-bottom: 10px;
        margin: 0 0 10px 0;
        cursor: pointer;

        .el-icon {
          margin-right: 10px;
        }

        &:last-child {
          border: none;
          margin-bottom: 0;
          padding: 0;
        }
      }
    }

    .label {
      display: flex;
      align-items: center;

      .el-icon {
        margin-right: 10px;
        cursor: move;
      }
    }

    >.el-icon {
      display: none;
    }
  }
}
</style>
